package lock;

import java.util.concurrent.TimeUnit;

/**
 * @author lyx
 * @Date 2021/6/27 15:07
 */
public class SpinlockTest1 {
    static Spinlock spinlock = new Spinlock();

    public static void main(String[] args) throws InterruptedException {
        //线程A拿到锁之后，3s后解锁
        new Thread(()->{
            spinlock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+" ====> 3s后解锁");
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlock.unlock();
            }

        },"A").start();


        new Thread(()->{
            spinlock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+" ====> 5s后解锁");
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlock.unlock();
            }
        },"B").start();
    }
}
